package com.cc.wechat.controller;

import com.cc.wechat.common.ApiResponse;
import com.cc.wechat.common.constants.ChatConstants;
import com.cc.wechat.dto.response.ChatSessionResponse;
import com.cc.wechat.service.ChatSessionService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 会话控制器
 * 处理会话相关的请求，包括创建会话、获取会话列表等
 */
@RestController
@RequestMapping("/api/chat/sessions")
@RequiredArgsConstructor
public class ChatSessionController {

    private final ChatSessionService chatSessionService;

    /**
     * 创建会话
     * @param params 包含targetId、type和lastMessage的参数Map
     * @return 会话信息
     */
    @PostMapping
    public ApiResponse<ChatSessionResponse> createOrGetSession(@RequestBody Map<String, Object> params) {
        Long targetId = Long.valueOf(params.get("targetId").toString());
        Integer type = (Integer) params.get("type");
        // 最近一条消息可能为空，所以需要判断
        String lastMessage = params.get("lastMessage") != null ? params.get("lastMessage").toString() : null;
        return ApiResponse.success(
                ChatConstants.Message.CREATE_SUCCESS,
                chatSessionService.createOrGetSession(targetId, type, lastMessage));
    }
    
    /**
     * 根据targetId获取会话
     * @param targetId 目标ID
     * @return 会话信息
     */
    @GetMapping("/{targetId}")
    public ApiResponse<ChatSessionResponse> getSessionByTargetId(@PathVariable Long targetId) {
        ChatSessionResponse session = chatSessionService.getSessionByTargetId(targetId);
        if (session == null) {
            return ApiResponse.success(ChatConstants.Message.SESSION_NOT_FOUND, null);
        }
        return ApiResponse.success(
            ChatConstants.Message.GET_SUCCESS,
            session
        );
    }

    /**
     * 获取会话列表
     * @return 会话列表
     */
    @GetMapping
    public ApiResponse<List<ChatSessionResponse>> getSessionList() {
        return ApiResponse.success(
            ChatConstants.Message.GET_LIST_SUCCESS,
            chatSessionService.getSessionList()
        );
    }

    /**
     * 设置会话置顶状态
     * @param sessionId 会话ID
     * @param isTop 是否置顶
     * @return 操作结果
     */
    @PutMapping("/{sessionId}/top")
    public ApiResponse<Void> setTopStatus(
            @PathVariable Long sessionId,
            @RequestParam Boolean isTop) {
        chatSessionService.setTopStatus(sessionId, isTop);
        return ApiResponse.success(ChatConstants.Message.SET_TOP_SUCCESS, null);
    }

    /**
     * 删除会话
     * @param sessionId 会话ID
     * @return 操作结果
     */
    @DeleteMapping("/{sessionId}")
    public ApiResponse<Void> deleteSession(@PathVariable Long sessionId) {
        chatSessionService.deleteSession(sessionId);
        return ApiResponse.success(ChatConstants.Message.DELETE_SUCCESS, null);
    }

    /**
     * 清空未读消息数
     * @param sessionId 会话ID
     * @return 操作结果
     */
    @PutMapping("/{sessionId}/read")
    public ApiResponse<Void> clearUnreadCount(@PathVariable Long sessionId) {
        chatSessionService.clearUnreadCount(sessionId);
        return ApiResponse.success(ChatConstants.Message.CLEAR_UNREAD_SUCCESS, null);
    }
} 